package com.it.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.aiReportClaim.ReportClaimApi;
import com.it.entity.dispatch.AiClaimDispatch;

import com.it.mapper.AiClaimDispatchMapper;
import com.it.service.AiClaimDispatchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.R;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 查勘调度表（报案受理后生成的查勘任务调度信息） 服务实现类
 * </p>
 *
 * @author lutao
 * @since 2025年09月19日
 */
@Service
public class AiClaimDispatchServiceImpl extends ServiceImpl<AiClaimDispatchMapper, AiClaimDispatch> implements AiClaimDispatchService {


    @Autowired
    private AiClaimDispatchMapper aiClaimDispatchMapper;

    @Autowired
    private ReportClaimApi reportClaimApi;

    @Override
    public R list(Integer pageNum, Integer pageSize, String reportId, String surveyWorkNo, Integer dispatchStatus) {
        PageHelper.startPage(pageNum, pageSize);

        List<AiClaimDispatch> list = aiClaimDispatchMapper.list(reportId, surveyWorkNo, dispatchStatus);
        PageInfo<AiClaimDispatch> pageInfo = new PageInfo<>(list);
        return R.Success("调度列表查询成功",pageInfo);
    }

    @Override
    public void export(Integer pageNum, Integer pageSize, String reportId, String surveyWorkNo, Integer dispatchStatus, HttpServletResponse response) {
        PageHelper.startPage(pageNum, pageSize);
        List<AiClaimDispatch> list = aiClaimDispatchMapper.list(reportId, surveyWorkNo, dispatchStatus);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("调度记录报表", "第一项"), AiClaimDispatch.class, list);
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("调度记录报表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            workbook.write(response.getOutputStream());
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public R undispatchList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AiClaimDispatch> list = aiClaimDispatchMapper.undispatchList();
        PageInfo<AiClaimDispatch> pageInfo = new PageInfo<>(list);
        return R.Success("未调度任务列表查询成功",pageInfo);
    }

    @Override
    @Transactional
    public void addDispatch(AiClaimDispatch aiClaimDispatch) {

        // 新增调度信息
        aiClaimDispatchMapper.insert(aiClaimDispatch);

        // 修改报案表状态
        reportClaimApi.dispatchUpdate(aiClaimDispatch.getReportId(), aiClaimDispatch.getReportStatus(), aiClaimDispatch.getWorkOutStatus());


    }

    @Override
    public R update(String reportNo, String surveyWorkNo, Integer dispatchStatus) {

        aiClaimDispatchMapper.update(reportNo,surveyWorkNo,dispatchStatus);

        return null;
    }


    @Override
    public List<AiClaimDispatch> selectByReportNo(String reportNo) {
        return aiClaimDispatchMapper.selectByReportNo(reportNo);
    }

}
